package com.syezon.wifikey.pwd.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.syezon.android.base.utils.ToastUtils;
import com.syezon.wifikey.R;
import com.syezon.wifikey.pwd.info.SaveWifiPwdInfo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/9/11.
 */

public class EditPwdDialog extends Dialog {

    @BindView(R.id.et_ssid)
    EditText mEtSsid;
    @BindView(R.id.et_pwd)
    EditText mEtPwd;
    @BindView(R.id.et_comment)
    EditText mEtComment;
    @BindView(R.id.tv_cancel)
    TextView mTvCancel;
    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;

    private SaveWifiPwdInfo mSaveWifiPwdInfo;
    private OpListener opListener;

    public EditPwdDialog(@NonNull Context context) {
        this(context, 0);
    }

    public EditPwdDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        setContentView(View.inflate(context, R.layout.dialog_edit_save_pwd, null));
        ButterKnife.bind(this);
    }

    public void setSaveWifiPwdInfo(SaveWifiPwdInfo saveWifiPwdInfo) {
        this.mSaveWifiPwdInfo = saveWifiPwdInfo;
        mEtSsid.setText(saveWifiPwdInfo.getSsid());
        mEtSsid.setSelection(mEtSsid.getText().toString().length());
        mEtPwd.setText(saveWifiPwdInfo.getPwd());
        mEtComment.setText(saveWifiPwdInfo.getComment());
    }

    public void setOpListener(OpListener opListener) {
        this.opListener = opListener;
    }

    @Override
    public void show() {
        getWindow().setGravity(Gravity.CENTER);
        getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        super.show();
    }

    @OnClick({R.id.tv_cancel, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_confirm:
                String pwd = mEtPwd.getText().toString();
                if(pwd.length() < 8){
                    ToastUtils.showShort(R.string.pwd_short_tip);
                    return;
                }
                if (opListener != null && mSaveWifiPwdInfo != null) {
                    SaveWifiPwdInfo newInfo = new SaveWifiPwdInfo();
                    newInfo.setId(mSaveWifiPwdInfo.getId());
                    newInfo.setConnectTime(mSaveWifiPwdInfo.getConnectTime());
                    newInfo.setSsid(mEtSsid.getText().toString());
                    newInfo.setPwd(pwd);
                    newInfo.setTime(System.currentTimeMillis());
                    newInfo.setComment(mSaveWifiPwdInfo.getComment());
                    opListener.onEdit(mSaveWifiPwdInfo , newInfo);
                }
                dismiss();
                break;
        }
    }

}
